<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel;
use think\Db;

class StoreGoodsController extends AdminBaseController
{
    /*
     * mt_Goods表
     */
    //店铺商品
    public function index()
    {   
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        /**搜索条件**/
        $res = $this->request->param();        
        $where=array();
        if(isset($res['goodsname']) && !empty($res['goodsname'])){
            $goodsname=$res['goodsname'];
            $where['g_goodsname'] = ['like', "%$goodsname%"];
        }
        if(isset($res['status']) && ($res['status'] !='quanbu')){
            $where['g_status'] = $res['status'];
        }
        $where['storeid']=$uinfo['store_id'];
        $list = Db::name('Goods')->where($where)->order("g_id DESC")->paginate(10);        
        // 获取分页显示
        $page = $list->render(); 
        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }
    public function shenhe()
    {
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        /**搜索条件**/
        $goodsname = $this->request->param('g_goodsname');
        $where=array();
        if ($goodsname){
            $where['g_goodsname'] = ['like', "%$goodsname%"];
        }
        $where['storeid']=$uinfo['store_id'];
        $where['g_status']=1;
        $list = Db::name('Goods')->where($where)->order("g_id DESC")->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }
    //编辑商品
    public function edit(){
        if ($this->request->isPost()) {
            $res  = $this->request->param();
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])) {
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            $data['g_goodsname']=$res['options']['g_goodsname'];
            if(isset($res['photo_urls'][0])){
                $data['g_pic']=$res['photo_urls'][0];
            }           
            $data['g_category']=$res['g_category'];
            $data['g_utime']=time();
            $data['g_status']=-1;
            $re=Db::name('Goods')->where(array('g_id'=>$res['g_id']))->update($data);
            if($re){
                $this->success("修改成功！", url("StoreGoods/index"));
            }else{
                $this->error("修改失败！");
            }
        }else{
            $id = $this->request->param("g_id");
            $data=Db::name('Goods')->where(array('g_id'=>$id))->find();
            //获取类别
            $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
            $this->assign("classify", $classify);
            $this->assign("data", $data);
            return $this->fetch();
        }
    }
    public function upStatus()
    {
        $intId     = $this->request->param("g_id");
        $intStatus = $this->request->param("g_status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("请选择要修改数据"));
        }
        $GoodsModel = new GoodsModel();
        $GoodsModel->isUpdate(true)->save(["g_status" => $intStatus], ["g_id" => $intId]);
        $this->success(lang("修改成功！"));
    }   
}